import type { User } from '@/types/user'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore(
  'user',
  () => {
    // 用户信息对象
    const user = ref<User>()

    // 修改用户信息
    const setUser = (u: User) => {
      user.value = u
    }

    // 删除用户信息
    const delUser = () => {
      user.value = undefined
    }

    // 存储登录成功之后跳转的路径
    const returnUrl = ref('')
    const setReturnUrl = (url: string) => {
      returnUrl.value = url
    }

    return { user, setUser, delUser, returnUrl, setReturnUrl }
  },
  {
    // 在把数据同步到仓库的同时会做本地持久化
    persist: true
  }
)
